package com.winglinks.message;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author wangzhong
 * @description: TODO
 * @date 2024/1/28 13:52
 */
@Component
public class DefalutMessageProductManager implements MessageProductManager, BeanPostProcessor {

    List<MessageProduct> messageProductList = new ArrayList<>();


    /**
     * 消息模板生成器
     *
     * @param model
     * @param deviceIdentification
     * @return
     */
    @Override
    public String generateMessage(String model, String deviceIdentification) {
        if(Objects.nonNull(messageProductList)){
            for (MessageProduct messageProduct : messageProductList) {
                if(messageProduct.isSupport(model)){
                    return messageProduct.generateMsg(deviceIdentification);
                }
            }
        }
        return null;
    }

    /**
     * 消息主题生成器
     *
     * @param model
     * @param gatewayId
     * @return
     */
    @Override
    public String generateTopic(String model, String gatewayId) {
        if(Objects.nonNull(messageProductList)){
            for (MessageProduct messageProduct : messageProductList) {
                if(messageProduct.isSupport(model)){
                    return messageProduct.generateTopic(gatewayId);
                }
            }
        }
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof MessageProduct){
            messageProductList.add((MessageProduct) bean);
        }
        return bean;
    }
}
